<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('发送时间')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select style="width: 25%" v-model="state" >
                <a-select-option v-for="(item, index) in sendList" :key="index" :value="item.value" @click="changeMemberState(item)">{{
                  item.title
                }}</a-select-option>
              </a-select>
              <a-select
                style="width: 75%"
                v-model="model.memberId"
                show-search
                :placeholder="$t('请输入会员手机号')"
                :filter-option="false"
                :not-found-content="null"
                @search="searchGoods"
              >
                <a-select-option key="99999999" :label="$t('标题')" value="99999999" disabled>
                  <span style="display: inline-block; width: 120px; color: #000; font-weight: bold">{{$t('手机号')}}</span>
                  <span style="display: inline-block; width: 150px; color: #000; font-weight: bold">{{$t('姓名')}}</span>
                  <span style="display: inline-block; width: 60px; color: #000; font-weight: bold">{{$t('代币')}}</span>
                </a-select-option>
                <a-select-option v-for="item in goodsList" :key="item.id" :label="item.name" :value="item.id" @click="member(item)">
                  <span style="display: inline-block; width: 120px">{{ item.phone }}</span>
                  <span style="display: inline-block; width: 150px">{{ item.name }}</span>
                  <span style="display: inline-block; width: 60px">{{
                    item.memberAccount.realCoin + item.memberAccount.presentCoin
                  }}</span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('短信内容')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea row="5" v-model="model.content" :placeholder="$t('请输入短信内容')"></a-textarea>
              <!-- <a-input v-model="model.content" placeholder="请输入短信内容"></a-input> -->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'BisSmsRecordForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
      },
      state:'0',

      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        memberId: [{ required: true, message: this.$t('请输入手机号') }],
        content: [{ required: true, message: this.$t('请输入短信内容') }],
      },
      url: {
        add: '/system/bisSmsRecord/send',
      },
      sendList: [],
      goodsList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      initDictOptions('bis_sms_record.send_type').then((res) => {
        if (res.success) {
          this.sendList = res.result
        }
      })
      this.visible = true
      this.searchGoods()
    },
    changeMemberState(item){
      if(item.value == 1){
        this.$message.warning(this.$t('正在开发中~'))
      }
    },
    member(item){
      console.log(item)
      this.model.phone = item.phone
    },
    searchGoods(value) {
      let query = {}
      query.memberInfo = value
      query.pageNo = 1
      query.pageSize = 20
      query.state = '0'
      getAction('/member/bisMemberInfo/list', query).then((res) => {
        if (res.success) {
          this.goodsList = res.result.records
        }
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.state == 1){
            this.$message.warning(this.$t('正在开发中~'))
            return
          }
          if(this.model.memberId == undefined){
            this.$message.warning(this.$t('请输入手机号'))
            return
          }
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>